import axios from 'axios'
import {getToken} from '@/utils/auth'

const service = axios.create({
    // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 60000 // request timeout
})

// request interceptor
service.interceptors.request.use(
    (config) => {
        let token = getToken();
        if (token) {
            config.headers['X-Token'] = token
        }
        if (process.env.NODE_ENV === 'development') {
            config.headers['X-Host'] = location.host
        }

        return config
    },
    (error) => {
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    (response) => {
        const res = response.data
        return res
    },
    (error) => {
        return Promise.reject(error)
    }
)

export default service
